/**
 * @Title springbootwebdemo系统
 * @Package com.bosft.sys.bin
 * @Description 鉴权配置
 * @Company 心医国际数字医疗系统（大连） 有限公司
 * @Copyright (c) 2016, 2017, bosft. All rights reserved.
 * @date 2017年10月18日 下午6:36:53 
 * @author WangNaiFei
 * @version 1.0
 */

package com.wheel.autoconfigure.shiro;

import java.util.Map;

import javax.servlet.Filter;

import com.wheel.autoconfigure.shiro.filter.ShiroLoginFilter;
import org.apache.shiro.spring.config.web.autoconfigure.ShiroWebFilterConfiguration;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.AbstractShiroWebFilterConfiguration;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;


/**
 * {@code ConfigerationOfShiro}
 * 
 * @Description: shiro配置
 *               <p>
 * @author WangNaiFei,wangnaifei@163.com
 * @date 2017年10月18日 下午6:36:53
 * @version 1.0
 * @since jdk1.7+
 */
@AutoConfigureBefore(ShiroWebFilterConfiguration.class)
@EnableConfigurationProperties(ShiroProperties.class)
public class ShiroFilterAutoConfiguration extends AbstractShiroWebFilterConfiguration {

	private ShiroProperties properties;

	/**
	 * @Title: ConfigerationOfShiroFilter
	 * @Description: 初始化设置配置参数
	 * @see @Autowired 不好用
	 */
	public ShiroFilterAutoConfiguration(ShiroProperties properties) {
		this.properties = properties;
	}

	@Bean
	protected ShiroFilterFactoryBean shiroFilterFactoryBean() {
		ShiroFilterFactoryBean shiroFilterFactoryBean = super.shiroFilterFactoryBean();
		String customloginUrl = properties.getLoginUrl();
		if (customloginUrl!=null) {
			shiroFilterFactoryBean.setLoginUrl(customloginUrl);
		}
		Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
		ShiroLoginFilter shiroLoginFilter = new ShiroLoginFilter();
		shiroLoginFilter.setDispatcherLoginConMatch(properties.getLoginSubmitUrl());
		filters.put("authc", shiroLoginFilter);
		return shiroFilterFactoryBean;
	}

	@Bean
	public ShiroFilterChainDefinition shiroFilterChainDefinition() {
		DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
		chainDefinition.addPathDefinitions(properties.getPathDefinitions());
		return chainDefinition;
	}

}
